<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 17:43
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Classification extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['topic_type_name'] = ['like',"%" . $keyword . "%"];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('topic_type')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('topic_type')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('topic_type_sort asc,topic_type_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $num = Db::table('up_topic')->where('topic_type',$vo['topic_type_id'])->count();
                $vo['num'] = $num;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function sort(){
        $param = input('');
        $res = Db::table('up_topic_type')->where('topic_type_id', $param['id'])->setField('topic_type_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('topic_type')->where('topic_type_id',$task_id)->delete();
        $num = Db::table('up_topic')->where('topic_type',$task_id)->count();
        if($num>0){
            return json(['code'=>100,'msg'=>'分类下面有话题不能删除']);
        }
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('topic_type')->where('topic_type_id',$task_id)->setField('topic_type_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('topic_type')->where('topic_type_id',$task_id)->setField('topic_type_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $topic_type_name =input('topic_type_name');
            $topic_type_sort= input('topic_type_sort');
            $topic_type_img= input('topic_type_img');
            $data['topic_type_name'] = $topic_type_name;
            $data['topic_type_sort'] = $topic_type_sort;
            $data['topic_type_img'] = $topic_type_img;
            $data['topic_type_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_topic_type')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $topic_type_name =input('topic_type_name');
            $topic_type_sort= input('topic_type_sort');
            $topic_type_img= input('topic_type_img');
            $data['topic_type_name'] = $topic_type_name;
            $data['topic_type_sort'] = $topic_type_sort;
            if(!empty($topic_type_img)){
                $data['topic_type_img'] = $topic_type_img;
            }
            $res = Db::table('up_topic_type')->where('topic_type_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('topic_type_id');
        $info= Db::table('up_topic_type')->where('topic_type_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}